package com.example.demo.repository;

import com.example.demo.model.Teacher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;

@Repository
public interface TeacherRepository extends JpaRepository<Teacher, Long> {
    Optional<Teacher> findByTeacherNo(String teacherNo);
    Optional<Teacher> findByUserId(Long userId);
    
    // 添加根据工号和姓名搜索的方法
    @Query("SELECT t FROM Teacher t JOIN User u ON t.userId = u.id WHERE " +
           "(:teacherNo IS NULL OR t.teacherNo LIKE CONCAT('%', :teacherNo, '%')) AND " +
           "(:name IS NULL OR u.realName LIKE CONCAT('%', :name, '%'))")
    Page<Teacher> findByTeacherNoContainingAndUserNameContaining(
        @Param("teacherNo") String teacherNo, 
        @Param("name") String name, 
        Pageable pageable);
}